using System;
using System.Collections.Generic;
using System.IO;
using TXLooker.Common.Core;
using TXLooker.WorkItemExtention.Properties;
using Microsoft.Office.Interop.Outlook;

namespace TXLooker.WorkItemExtention.Core
{
    class WorkItemExtentionDataProvider
    {
        public WorkItemExtentionDataProvider()
        { }

        private List<ProjectStore> m_storedProjectAndUsers;

        public List<ProjectStore> StoredUsers
        {
            get { return this.m_storedProjectAndUsers; }
            set { this.m_storedProjectAndUsers = value; }
        }

        public string AppStoreFolderPath
        {
            get
            {
                if (m_storeFolderPath == null)
                {
                    m_storeFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                    m_storeFolderPath = Path.Combine(m_storeFolderPath, Resources.APPLICATION_NAME);

                    if (!Directory.Exists(m_storeFolderPath))
                        Directory.CreateDirectory(m_storeFolderPath);
                }
                return m_storeFolderPath;
            }
        }
        private string m_storeFolderPath = null;

        private string m_storedProjectAndUsersFileName = "Users.txp";

        public string StoredUsersFilePath
        {
            get { return this.AppStoreFolderPath + "\\" + this.m_storedProjectAndUsersFileName; }
        }

        public void Serialize(string path, Type objectType, object obj)
        {
            using (FileStream FS = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
            {
                System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer(objectType);
                formatter.Serialize(FS,obj);
            }
        }

        private NameSpace m_Session;

        public NameSpace Session
        {
            get { return this.m_Session; }
            set 
            {
                if (object.ReferenceEquals(null, value))
                    throw new NullReferenceException();
                this.m_Session = value;
            }
        }

        //public string ContentPatternEditorFile
        //{
        //    get 
        //    {
        //        return this.AppStoreFolderPath
        //    }
        //}

        public object Deserialize(string path, Type objectType)
        {
            try
            {
                if (!File.Exists(path))
                    return null;
                object retObject = null;
                using (FileStream FS = new FileStream(path, FileMode.Open))
                {
                    System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer(objectType);
                    retObject = formatter.Deserialize(FS);
                }

                return retObject;
            }
            catch
            {
                return null;
            }
        }
    }
}
